const app = getApp();
const util = require('../../utils/util.js')

/* 页面的 js */
Page({
    data: {
        regionValue: [],
        showRegion: false,
        areaName:'',
        areaAddr:'',
    },
    onShow: function (){
        var that = this;
        wx.getSetting({
            success: (res) => {
                if (!res.authSetting['scope.userLocation']){
                    that.setData({
                        locationData:false
                    })
                } else if (res.authSetting['scope.userLocation']){
                    that.setData({
                        locationData:true
                    })
                }

            }
        })
    },
    onLoad: function () {
        wx.setNavigationBarTitle({
            title: '添加区域'
        })
    },
    chooseRegion: function() {
        this.setData({
            showRegion: true,
        });
    },
    emitHideRegion: function(e) {
        console.log(e)
        this.setData({
            showRegion: e.detail.showRegion,
            regionValue: e.detail.regionValue,
        });
    },

    bindAreaInput: function (e) {
        this.setData({
            areaName: e.detail.value
        })
    },
    bindAddr: function (e) {
        this.setData({
            areaAddr: e.detail.value
        })
    },


    addAreaCheck: function (){
        console.log(this.data.regionValue)
        if(this.data.regionValue.length < 3 || !this.data.regionValue[2].id) {
            wx.showModal({
                title: '请选择区域所在区县',
                showCancel: false,
            })
        }else if(!this.data.areaName){
            wx.showModal({
                title: '请填写区域名称',
                showCancel: false,
            })
        }else if(!this.data.longitude){
            wx.showModal({
                title: '详细地址不可为空',
                showCancel: false,
            })
        }else {
            this.addArea()
        }
    },
    addArea: function () {
        wx.showLoading({
            title: '加载中'
        })
        console.log(this.data.areaName)
        var that = this
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/active/addArea',
            // url: 'http://192.168.90.189:8080/vending-admin/active/addArea',
            method: 'GET',
            data: {
                token: app.globalData.token,
                areaname: that.data.areaName,
                county: that.data.regionValue[2].id,
                address: that.data.address + that.data.addressName,
                longitude:that.data.longitude,
                latitude:that.data.latitude,

            },
            header: {
                'Accept': 'application/json'
            },
            success: function (res) {
                wx.hideLoading();
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    wx.showToast({
                        title: '添加成功',
                        icon: 'success',
                        duration: 3000
                    });
                    setTimeout(function(){
                        wx.navigateBack({
                            delta: 1
                        })
                    },1000)

                }
            }
        })
    },
    bindChangeAddress: function () {
        var that = this;
        wx.getSetting({
            success: (res) => {
                if (!res.authSetting['scope.userLocation']){
                    that.setData({
                        locationData:false
                    })
                    wx.showModal({
                        title:'监测到您未授权翁之意使用位置，请打开',
                        showCancel: false,
                    })
                } else if (res.authSetting['scope.userLocation']){
                    that.setData({
                        locationData:true
                    })
                    wx.navigateTo({
                        url: "/pages/get-position/get-position"
                    });
                }

            }
        })

    }
});
